import { useNavigate } from 'react-router-dom';

export const useAuth = () => {
  const navigate = useNavigate();

  const isAuthenticated = () => {
    const token = localStorage.getItem('auth_token');
    const authTime = localStorage.getItem('auth_time');
    
    if (!token || !authTime) {
      return false;
    }
    
    // 检查token是否过期（24小时）
    const now = Date.now();
    const time = parseInt(authTime, 10);
    const maxAge = 24 * 60 * 60 * 1000;
    
    if (now - time > maxAge) {
      logout();
      return false;
    }
    
    return true;
  };

  const logout = () => {
    localStorage.removeItem('auth_token');
    localStorage.removeItem('auth_time');
    navigate('/login', { replace: true });
  };

  return {
    isAuthenticated,
    logout
  };
};

